'use strict';

const Service = require('egg').Service;
const nodemailer = require('nodemailer');


class SendMailService extends Service {
  /**
   * 
   * @param {邮箱} mailbox 
   * @param {联系方式} information 
   * @param {说明} intro 
   */
  sendMail = (mailbox, information, intro) => {
    return new Promise((resolve, reject) => {
      let transporter = nodemailer.createTransport({
        host: 'smtp.qq.com',
        port: 465,
        secure: true,
        service: 'qq', // 使用QQ邮件服务商  
        auth: {
          user: '769502651@qq.com', // 发送方邮箱地址  
          pass: 'yuressbeenapbfhb' // 发送方邮箱密码（此处应为授权码）  
        }
      });

      let mailOptions = {
        from: '769502651@qq.com', // 发送方邮箱地址  
        to: 'ryan.suying@gmail.com', // 接收方邮箱地址  
        subject: '发送联系方式', // 邮件主题  
        html: `
          <h3>邮箱：</h3><span>${mailbox}</span>
          <h3>联系方式：</h3><span>${information}</span>
          <h3>说明：</h3><span>${intro}</span>
        ` // 邮件内容  
      };
      transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
          reject(error)
        } else {
          resolve(info)
        }
      });
    })
  }
}

module.exports = SendMailService;
